#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QString>
#include <QtCore/QTextCodec>
#include <QtCore/QTextStream>
#include <QtCore/QDataStream>
#include <QtCore/QStringList>
#include <QtCore/QCoreApplication>

#include "linal/vector.h"
#include "fem/trianglenet.h"
#include "fem/trianglenetsearch.h"

//================================================================================================================================
//================================================================================================================================
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));

    //****************************************************************************************************************************
    //****************************************************************************************************************************
    QFile ofile("G:/static_t_int.dat");
    QTextStream ostream(&ofile);

    ofile.open(QFile::WriteOnly | QFile::Text);

    QDir input_dir(QObject::tr("G:/Результаты/NSTU/output_2s_1t"));
    foreach(QString filename_nvtr, input_dir.entryList(QStringList() << "*nvtr.dat"))
    {
        QString filename_rz = filename_nvtr.left(filename_nvtr.size() - 8) + "rz.dat";
        QString filename_t = filename_nvtr.left(filename_nvtr.size() - 8) + "T.dat";

        linal::Vector t(input_dir.absolutePath() + "/" + filename_t);
        fem::TriangleNet net(input_dir.absolutePath() + "/" + filename_rz,
                             input_dir.absolutePath() + "/" + filename_nvtr);

        double integral = 0;
        for(int i = 0; i < net.numOfElements(); i++)
        {
            double detD = (net.point(i, 1).x() - net.point(i, 0).x())*(net.point(i, 2).y() - net.point(i, 0).y()) -
                    (net.point(i, 2).x() - net.point(i, 0).x())*(net.point(i, 1).y() - net.point(i, 0).y());

            for(int j = 0; j < 3; j++)
                for(int k = 0; k < 3; k++)
                    if(j == k)
                        integral += t[net.pointIndex(i, j)]*net.point(i, k).x()*qAbs(detD)/12.;
                    else
                        integral += t[net.pointIndex(i, j)]*net.point(i, k).x()*qAbs(detD)/24.;
        }

        ostream << filename_nvtr.remove("nvtr.dat") << " " << integral << endl;
    }

    ofile.close();

}
